package com.etms.controller;

import com.etms.pojo.SysUser;
import com.etms.service.UserService;
import com.etms.vo.PageParmas;
import com.etms.vo.Result;
import com.etms.vo.UserParmas;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("users")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("login")
    public Result userLogin(@RequestBody UserParmas user, HttpSession session){
        Result result = userService.userLogin(user);
        SysUser u = (SysUser) result.getData();
        session.setAttribute("user",u);
        return result;
    }

    @GetMapping("logout")
    public Result logout(HttpSession session){
        session.removeAttribute("user");
        return Result.successed("退出登录!",null);
    }
    @GetMapping("getOrder")
    public int getOrder(HttpSession session){
        SysUser user =(SysUser) session.getAttribute("user");
        if(user==null){
            return 3;
        }
        return user.getSault();//返回权限
    }


    @PostMapping("getList")
    public Result getList(@RequestBody PageParmas pageParmas){
        return userService.getList(pageParmas);
    }

    @GetMapping("getUserById/{id}")
    public Result getById(@PathVariable("id") Integer id){
        return userService.findById(id);
    }

    @GetMapping("delById/{id}")
    public Result delById(@PathVariable("id") Integer id){
        return userService.delById(id);
    }

    @PostMapping("Update")
    public Result UpdateUser(@RequestBody SysUser user){
        return userService.updateOrAdd(user);
    }
}
